package cn.jdy.plugins.web;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.Search;
import cn.jdy.plugins.service.SearchService;

/**
 * 搜索
 * @author songql
 *
 */
@Controller
@RequestMapping("/search")
public class SearchCtrl extends BaseCtrl {
  @Autowired
  private SearchService searchService;

  /**
  * 内容搜索
  * @param search
  * @param keywords
  * @return
  */
  @RequestMapping("/searchbykey")
  public ModelAndView searchbykey(Search search,String keywords) {
    ModelAndView mv = new ModelAndView("/searchList.jsp");
    try {
      //设置分页信息
      if(search.getPagesize() == 0) search.setPagesize(10);                //默认长度10
      if(search.getPageNo()<=0) search.setPageNo(1);                                  //默认页数1
      search.setPagestart((search.getPageNo() - 1) * search.getPagesize());  //显示起始索引

//    原查询
    mv.addObject("dataList", searchService.queryByLike(search,keywords));
    mv.addObject("total", searchService.queryCount(keywords));

//      市公安局
      /*useless
//      mv.addObject("dataList", searchService.queryByPoliceN(search,keywords));
//      mv.addObject("dataListW", searchService.queryByPoliceW(search,keywords));
//      mv.addObject("dataListNS", searchService.queryByPoliceNS(search,keywords));
//      mv.addObject("dataListF", searchService.queryByPoliceF(search,keywords));
//      mv.addObject("dataListFA", searchService.queryByPoliceFA(search,keywords));
//      mv.addObject("dataListC", searchService.queryByPoliceC(search,keywords));
      */
//      mv.addObject("dataList", searchService.queryByPolice(search,keywords));
//      mv.addObject("total", searchService.queryCountP(keywords));
      
      mv.addObject("keywords", keywords);
      mv.addObject("pageNo", search.getPageNo());
      mv.addObject("size", search.getPagesize());
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "搜索异常", e);
    }
    return mv;
  }
  
  
  /**
   * 市局搜索
   * @param search
   * @param keywords
   * @return
   */
  @RequestMapping("/searchBy")
  public ModelAndView searchby(Search search,String keywords) {
    ModelAndView mv = new ModelAndView("/searchList.jsp");
  //襄阳市公安局，需要加入当前时间段周数
	Date datetime = new Date();
	int weekdays = datetime.getDay();
	mv.addObject("weekdays",weekdays);
    
    try {
      //设置分页信息
      if(search.getPagesize() == 0) search.setPagesize(10);                //默认长度10
      if(search.getPageNo()<=0) search.setPageNo(1);                                  //默认页数1
      search.setPagestart((search.getPageNo() - 1) * search.getPagesize());  //显示起始索引

//      市公安局
//      useless
      mv.addObject("dataList", searchService.queryByPoliceN(search,keywords));
      mv.addObject("dataListW", searchService.queryByPoliceW(search,keywords));
      mv.addObject("dataListNS", searchService.queryByPoliceNS(search,keywords));
      mv.addObject("dataListF", searchService.queryByPoliceF(search,keywords));
      mv.addObject("dataListFA", searchService.queryByPoliceFA(search,keywords));
      mv.addObject("dataListC", searchService.queryByPoliceC(search,keywords));
    
//      mv.addObject("dataList", searchService.queryByPolice(search,keywords));
      mv.addObject("total", searchService.queryCountP(keywords));
      
      mv.addObject("keywords", keywords);
      mv.addObject("pageNo", search.getPageNo());
      mv.addObject("size", search.getPagesize());
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "搜索异常", e);
    }
    return mv;
  }
  
}
